package com.bangdream.michelia.application;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import com.alibaba.fastjson.JSON;
import com.bangdream.michelia.entity.UserBean;
import com.bangdream.michelia.utils.L;
import com.bangdream.michelia.utils.PublicResources;
import com.bangdream.michelia.utils.SPUtils;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;


/**
 * Created by Mr.Wang on 2017/10/18.
 * 909732184@qq.com
 * 记录用户的登录状态信息等
 */

public class AppCurrentUser {
    private static final String TAG = "AppCurrentUser";
    private static volatile AppCurrentUser mInstance;

    private String sessionid;

    private AppCurrentUser() {
    }

    public static AppCurrentUser getInstance(){
        if (mInstance == null) {
            synchronized (AppCurrentUser.class) {
                if (mInstance == null) {
                    mInstance = new AppCurrentUser();
                }
            }
        }
        return mInstance;
    }
    //获取当前登录的Token
    public String getToken()
    {
        String stToken = (String) SPUtils.get(MainApplication.getContext(), PublicResources.SpKeyUserToken,"");
        L.d("获取到的用户token为："+stToken);
        return stToken;
    }


    public void setToken(String token){
        SPUtils.put(MainApplication.getContext(),PublicResources.SpKeyUserToken,token);
    }
    //获取当前登录的用户信息
    public UserBean getUserInfo()
    {
        String stUserInfo = (String) SPUtils.get(MainApplication.getContext(), PublicResources.SpKeyUserInfo,"");
        L.d("获取到的用户信息为："+stUserInfo);
        UserBean userBean = null;
        try {
            userBean = JSON.parseObject(stUserInfo,UserBean.class);
        }catch (Exception e){
            userBean=new UserBean();
            L.e(e.toString());
        }
        return userBean;
    }

    //查询当前状态是否已登录
    public boolean isSign()
    {
        boolean isSign = false;
        String stToken = (String) SPUtils.get(MainApplication.getContext(), PublicResources.SpKeyUserToken,"");
        if(stToken!=null&&stToken.length()>0){
            isSign = true;
        }
        return isSign;
    }

    //退出登录
    public boolean EndSign()
    {
        try {
            //退出时删除token
            SPUtils.remove(MainApplication.getContext(),PublicResources.SpKeyUserToken);
            SPUtils.remove(MainApplication.getContext(),PublicResources.SpKeyUserInfo);
            SPUtils.remove(MainApplication.getContext(),"userName");
            SPUtils.remove(MainApplication.getContext(),"pwd");
            SPUtils.remove(MainApplication.getContext(),"doMain");
            SPUtils.remove(MainApplication.getContext(),"domainName");
            removeThird();
            return true;
        }catch(Exception e)
        {
            L.d(TAG,"EndSign获取登录用户数据失败"+e);
            return false;
        }
    }

    //清除第三方授权信息 qq -微信
    private void removeThird(){
        Platform qq = ShareSDK.getPlatform(QQ.NAME);
        if (qq.isAuthValid()) {
            qq.removeAccount(true);//执行此操作就可以移除掉本地授权状态和授权信息
        }
        Platform weichat = ShareSDK.getPlatform(Wechat.NAME);
        if (weichat.isAuthValid()) {
            weichat.removeAccount(true);//执行此操作就可以移除掉本地授权状态和授权信息
        }
    }
}
